package org.curiouscreature.android.shelves.activity;

import java.util.ArrayList;
import java.util.List;

import org.curiouscreature.android.shelves.R;
import org.curiouscreature.android.shelves.provider.BookStoreFactory;
import org.curiouscreature.android.shelves.provider.BooksManager;
import org.curiouscreature.android.shelves.provider.BooksStore;
import org.curiouscreature.android.shelves.scan.ScanIntent;
import org.curiouscreature.android.shelves.util.PublicUtilites;
import org.curiouscreature.android.shelves.util.UIUtilities;
import org.curiouscreature.android.shelves.util.UserTask;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ListBooksActivity extends Activity {
    private AddTask mAddTask;
    private List<BookViewHolder> downList = new ArrayList<BookViewHolder>();
    private Menu menu;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_book_adapter);
		
    	final ListBooksAdapter adapter = new ListBooksAdapter(this);
    	ListView listView = (ListView)findViewById(R.id.list_book_adapter);
    	listView.setAdapter(adapter);
    	
    	registerForContextMenu(listView);
    	listView.setOnItemClickListener(new BookViewer());
 
 
	}
	
 

    //内部类用来监听ListView的点击事件
	class BookViewer implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            onView(((BookViewHolder) view.getTag()).bookId);
            
        }
    }
    

	
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenu.ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle("标题");
        getMenuInflater().inflate(R.menu.book, menu);
    }
    
    

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)
                item.getMenuInfo();
        final BookViewHolder holder = (BookViewHolder) info.targetView.getTag();

        switch (item.getItemId()) {
            case R.id.context_menu_item_view:
                onView(holder.bookId);
                return true;
            case R.id.context_menu_item_buy:
                onBuy(BooksManager.findBook(getContentResolver(), holder.bookId));
                return true;
            case R.id.context_menu_item_delete:
                onDelete(holder.bookId);
                return true;
        }

        return super.onContextItemSelected(item);
    }
   
    //按Menu时触发的事件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.shelves, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        final boolean scanAvailable = ScanIntent.isInstalled(this);

        MenuItem item;
        item = menu.findItem(R.id.menu_item_add);
        item.setEnabled(scanAvailable && (mAddTask == null ||
                mAddTask.getStatus() == UserTask.Status.FINISHED));

        item = menu.findItem(R.id.menu_item_show_style);
        item.setEnabled(scanAvailable);

        return super.onPrepareOptionsMenu(menu);
    }
 
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_add_search:
            	 PublicUtilites.onAddSearch();
                return true;
            case R.id.menu_item_add:
                onAdd();
                return true;
            case R.id.menu_item_check:
                onCheck();
                return true;
            case R.id.menu_item_search:
                onSearchRequested();
                return true;
            case R.id.menu_item_settings:
                onSettings();
                return true;
            case R.id.menu_item_show_style:
            	onShowStyle();
            	return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    private void startScan(int code) {
        try {
            final Intent intent = new Intent(ScanIntent.INTENT_ACTION_SCAN);
            intent.putExtra(ScanIntent.INTENT_EXTRA_SCAN_MODE,
                    ScanIntent.INTENT_EXTRA_PRODUCT_MODE);
            startActivityForResult(intent, code);
        } catch (ActivityNotFoundException e) {
            UIUtilities.showToast(this, R.string.error_missing_barcode_scanner, true);
        }
    }
    public void onAddSearch() {
        AddBookActivity.show(this);
    }
    
    private void onAdd() {
        startScan(ShelvesActivity.REQUEST_SCAN_FOR_ADD);
    }

    private void onCheck() {
        startScan(ShelvesActivity.REQUEST_SCAN_FOR_CHECK);
    }
    
    private void onSettings() {
        SettingsActivity.show(this);
    }
 
 
    private void onShowStyle(){
    	startActivity(new Intent(this,ListBooksActivity.class));
    	//finish();
 
    }
    private void onBuy(BooksStore.Book book) {
        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(book.getDetailsUrl()));
        startActivity(intent);
    }

    private void onDelete(String bookId) {
        if (BooksManager.deleteBook(getContentResolver(), bookId)) {
            UIUtilities.showToast(this, R.string.success_book_deleted);
        }
    }
    
    private void onView(String bookId) {
        BookDetailsActivity.show(this, bookId);
    }
    
    private class AddTask extends UserTask<String, Void, BooksStore.Book> {
        private final Object mLock = new Object();
        private String mBookId;

        @Override
        public void onPreExecute() {
 

        }

        String getBookId() {
            synchronized (mLock) {
                return mBookId;
            }
        }

        public BooksStore.Book doInBackground(String... params) {
            synchronized (mLock) {
                mBookId = params[0];
            }
            return BooksManager.loadAndAddBook(getContentResolver(), mBookId,
                    BookStoreFactory.get(ListBooksActivity.this));
        }

 
    }

}
